package cn.tedu.gateway.filter;

import com.alibaba.nacos.api.utils.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {

    //全局过滤器判断   业务逻辑
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //exchange  存放了请求相关信息  token=admin
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if (!StringUtils.equals(token,"admin")){
            //做提示输出
            System.out.println("签权失败");
            //设置响应状态  500
            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            //返回失败响应
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
//当前过滤器执行的优先级   越低  优先级越高
    @Override
    public int getOrder() {
        return 0;
    }
}
